简介
Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.
They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.
Snackbars can contain an action which is set via {@link #setAction(CharSequence, android.view.View.OnClickListener)}. To be notified when a snackbar has been shown or dismissed, you can provide a {@link Callback} via {@link #setCallback(Callback)}.
翻译: Snackbar为用户的一个操作提供轻量级的反馈,他会在手机屏幕的底部或者是大设备的左下方显示一个简洁的消息。Snackbar出现在屏幕所有元素的上面,显示一小会。 Snackbar显示一会后会自动消失,用户点击其他地方的时候也会消失,尤其,当显示一个新的界面的时候,Snackbar会滑出界面。 Snackbar 可以通过setAction(CharSequence, android.view.View.OnClickListener) 来设置一个点击事件,同时也可以通过setCallback(Callback)来监听snackbar的显示和消失。
类结构 Snackbar中Public属性列表
方法或接口
意义
Callback
抽象函数用来设置SnackBar显示和消失的回调
make
创建SnackBar
setAction
设置ActionView的响应事件
show
显示SnackBar
dismiss
收起SnackBar
setActionTextColor
设置ActionView的字体颜色
setText
设置TextView的显示内容
setDuration
设置SnackBar的显示时间
getDuration
获取SnackBar的显示时间
getView
获取SnackBar的View视图
setCallback
设置显示和消失的事件监听
isShown
是否已经显示
shownOrQueued
是否已经显示或者在队列中等待显示
整理而言的话,感觉Snackbar就是Toast的一个升级版本,使用的方式和Toast也比较相似。
创建Snackbar之后一定要记得调用show()方法
基础使用 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package mraz.com.snackdemo;import android.os.Bundle;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View .OnClickListener { RelativeLayout rootLayout; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); rootLayout = (RelativeLayout) findViewById(R.id.root_layout); Button shortButton = (Button) findViewById(R.id.short_button); Button longButton = (Button) findViewById(R.id.long_button); shortButton.setOnClickListener(this ); longButton.setOnClickListener(this ); } @Override public void onClick (View view) { switch (view.getId()) { case R.id.short_button: final Snackbar shortSnackbar = Snackbar.make(rootLayout, " Short SnackBar" , Snackbar.LENGTH_SHORT); shortSnackbar.setAction("Click" , new View.OnClickListener() { @Override public void onClick (View view) { Toast.makeText(rootLayout.getContext(), "Shoort Snackbar onClick" , Toast.LENGTH_SHORT).show(); } } ); shortSnackbar.setActionTextColor(getResources().getColor(R.color.colorPrimaryDark)); shortSnackbar.show(); break ; case R.id.long_button: Snackbar longSnackbar = Snackbar.make(rootLayout, " Long SnackBar" , Snackbar.LENGTH_LONG); longSnackbar.setCallback(new SnackCallback()); longSnackbar.show(); break ; } } class SnackCallback extends Snackbar .Callback { @Override public void onDismissed (Snackbar snackbar, int event) { super .onDismissed(snackbar, event); Toast.makeText(rootLayout.getContext(), "SnackBar onDismissed" , Toast.LENGTH_SHORT).show(); } @Override public void onShown (Snackbar snackbar) { super .onShown(snackbar); Toast.makeText(rootLayout.getContext(), "SnackBar onShown" , Toast.LENGTH_SHORT).show(); } } }
效果